package com.library.weidget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.jtjrenren.android.taxi.passenger.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/3/2.
 */
public class RaderProcessViewCustome extends FrameLayout {
    public interface TimeListener {
        void timeOver();
    }

    private static int JD_JG = 1000; //时间刻度间隔
    private static int MAX_TIME = 60; //默认查找时间

    private RoundHView roundHView;
    private ImageView imageView;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (timeListener != null)
                        timeListener.timeOver();
                    stopScan();
                    break;
            }
        }
    };

    private Timer timer;
    private int process = 0;

    private TimeListener timeListener;

    public RaderProcessViewCustome(Context context) {
        super(context);
        initView(context);
    }

    public RaderProcessViewCustome(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public RaderProcessViewCustome(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.custome_radarprocess, this);

        roundHView = (RoundHView) findViewById(R.id.radar_yh);
        imageView = (ImageView) findViewById(R.id.radar_scan);
    }

    float currentValue = 0;
    ObjectAnimator rotation;

    public void startScan() {
        process = 0;

        timer = new Timer();
        timer.schedule(new myTask(), 0, JD_JG);

        rotation = ObjectAnimator.ofFloat(imageView, "Rotation",
                currentValue - 360, currentValue);
        rotation.setDuration(2300);
        rotation.setRepeatCount(ObjectAnimator.INFINITE);
        rotation.setInterpolator(new LinearInterpolator());
        rotation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentValue = (Float) animation.getAnimatedValue();
            }
        });
        rotation.start();
    }

    public void stopScan() {
        if (timer != null)
            timer.cancel();
        if (rotation != null)
            rotation.end();
    }

    public void setTimeListener(TimeListener timeListener) {
        this.timeListener = timeListener;
    }

    class myTask extends TimerTask {

        @Override
        public void run() {
            roundHView.refresh(process * 100 / MAX_TIME);
            process++;
            if (process > MAX_TIME)
                handler.sendEmptyMessage(0);
        }
    }
}
